package com.github.zzay.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.zzay.care.dto.EmployeeDto;
import com.github.zzay.care.entity.Employee;
import com.github.zzay.care.mapper.EmployeeMapper;
import com.github.zzay.care.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className EmployeeServiceImpl
 * @description Implementation class for "EmployeeService"
 * @create 2022/07/07 00:07
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    /**
     * Employee Service.
     */
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * Delete an employee's information.
     *
     * @param idCard ID card number
     * @return Operation result
     */
    public Boolean deleteInfo(String idCard) {
        Map<String, Object> map = new HashMap<>();
        EmployeeDto employee = employeeMapper.selectByIdCard(idCard);
        if (employee == null) {
            return false;
        }
        map.put("id_card", idCard);
        employeeMapper.deleteByMap(map);
        return true;
    }

    /**
     * Update an employee's information.
     *
     * @param employeeDto Employee Dto
     * @return Operation result
     */
    public Boolean updateInfo(EmployeeDto employeeDto) {
        EmployeeDto employee = employeeMapper.selectByIdCard(employeeDto.getIdCard());
        if (employee == null) {
            return false;
        }
        LambdaUpdateWrapper<Employee> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Employee::getIdCard, employee.getIdCard())
                .set(Employee::getSex, employeeDto.getSex())
                .set(Employee::getPhone, employeeDto.getPhone())
                .set(Employee::getBirthday, employeeDto.getBirthday())
                .set(Employee::getHireDate, employeeDto.getHireDate())
                .set(Employee::getResignDate, employeeDto.getResignDate());
//        employee.setSex(employeeDto.getSex());
//        employee.setPhone(employeeDto.getPhone());
//        employee.setBirthday(employeeDto.getBirthday());
//        employee.setHireDate(employeeDto.getHireDate());
//        employee.setResignDate(employeeDto.getResignDate());
//        employee.setProfilePhoto(employeeDto.getProfilePhoto());
//        employee.setImageDir(employeeDto.getImageDir());
        return employeeMapper.update(null, lambdaUpdateWrapper) != 0;
    }

    /**
     * Get all information.
     *
     * @return A list of all information
     */
    public List<EmployeeDto> getAll() {
        return employeeMapper.selectAll();
    }

    /**
     * Get an employee's information with the given id card number.
     *
     * @param idCard ID card number
     * @return Employee's information
     */
    public EmployeeDto getInfoById(String idCard) {
        return employeeMapper.selectByIdCard(idCard);
    }

    /**
     * Get an employee's information with the given name.
     *
     * @param name Name
     * @return Employee's information
     */
    public EmployeeDto getInfoByName(String name) {
        return employeeMapper.selectByName(name);
    }

}
